<?php
/**
 * Created by Unknown
 *
 *            ┏┓　　  ┏┓+ +
 *           ┏┛┻━━━━━┛┻┓ + +
 *           ┃         ┃ 　
 *           ┃   ━     ┃ ++ + + +
 *          ████━████  ┃+
 *           ┃　　　　　 ┃ +
 *           ┃　　　┻　　┃
 *           ┃　　　　　 ┃ + +
 *           ┗━┓　　　┏━┛
 *             ┃　　　┃　　　　
 *             ┃　　　┃ + + + +
 *             ┃　　　┃    Code is far away from bug with the alpaca protecting
 *             ┃　　　┃ + 　　　　        神兽保佑,代码无bug
 *             ┃　　　┃
 *             ┃　　　┃　　+
 *             ┃     ┗━━━┓ + +
 *             ┃         ┣┓
 *             ┃ 　　　　　┏┛
 *             ┗┓┓┏━━┳┓┏━┛ + + + +
 *              ┃┫┫  ┃┫┫
 *              ┗┻┛  ┗┻┛+ + + +
 * User: MotherFucker
 * Date: 2016-08-31
 * Time: 上午 10:55
 */

namespace Common;

class Common{
    static private $__string = "abcdefghijklmnopqrstuvwxyz0123456789";
    static private $__number = "0123456789";
    static private $__letter = "abcdefghijklmnopqrstuvwxyz";

    public function Func(){
        return __METHOD__;
    }

    public static function getRandom($type = 'mix', $length = 8){
        $length = !empty($length) && is_numeric($length) ? intval($length) : 8;

        $random_str = null;
        if($type == 'mix'){
            $type = self::$__string;
        }else if($type == 'str'){
            $type = self::$__letter;
        }else if($type == 'num'){
            $type = self::$__number;
        }
        $max = strlen($type)-1;

        for($i=0; $i<$length; $i++){
            $random_str .= $type[rand(0,$max)];//rand($min,$max)生成介于min和max两个数之间的一个随机整数
        }

        return $random_str;
    }

    /**
     * 对象转成数组
     * @param $object
     * @return mixed
     */
    public static function objToArr($object){

        $attribute_Arr = [];
        if (empty($object->data)){
            foreach (get_object_vars($object) as $index => $item) {
                $attribute_Arr[substr($index,1)] = $item;
            }
        }else{
            $attribute_Arr = get_object_vars($object)['data'];
        }

        return $attribute_Arr;
    }

    public static function getTree($result,$pid =0, $level = 0){
        static $treeList = [];
        foreach ($result as $index => $option){
            if ($option['typeid'] ==$pid){
//                $treeList[$option['id']] =['text' => $option['title'],'url' => $option['name']];
                $option['level'] = $level;
                //把数组放到list中
                $treeList[] = $option;
                //把这个节点从数组中移除,减少后续递归消耗
                unset($result[$index]);
                //开始递归,查找父ID为该节点ID的节点,级别则为原级别+1
                self::getTree($result, $option['id'], $level+1);
            }
        }
        return $treeList;
    }

    /**
     * 将字符解析成数组
     * @param $str
     */
    public static function parseParams($str)
    {
        $arrParams = [];
        parse_str(html_entity_decode(urldecode($str)), $arrParams);
        return $arrParams;
    }

}
